package org.jeecg.modules.mstr.controller;

import com.alibaba.druid.util.StringUtils;
import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.jeecg.common.api.vo.Result;
import org.jeecg.modules.mstr.entity.Mstr;
import org.jeecg.modules.mstr.service.IMstrApiService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

/**
 * @author liupan
 */
@RestController
@RequestMapping("mstr")
@Slf4j
public class MstrApiControll {

    @Autowired
    private IMstrApiService iMstrApiService;

    @PostMapping("login")
    @ApiOperation("MSTR登录接口")
    public Result<JSONObject> login(@RequestBody Mstr mstr){
        Result<JSONObject> result = new Result<JSONObject>();
        try{
            String token = iMstrApiService.login(mstr.getUsername(), mstr.getPassword());
            if(!StringUtils.isEmpty(token)){
                result.setCode(201);
                result.setMessage(token);
            }else{
                result.error500("没有获取token!");
            }
        }catch (Exception ex){
            result.error500(ex.getMessage());
        }
        return result;
    }

    @PostMapping("report")
    @ApiOperation("根据reportId获取report对象")
    public Result<JSONObject> getReportByReportId(@RequestBody Mstr mstr) throws Exception{
            Result<JSONObject> result = new Result<JSONObject>();
        try{
            JSONObject resultObj = iMstrApiService.report(mstr.getToken(), mstr.getProjectId(), mstr.getReportId());
            result.setCode(resultObj.getInteger("statusCode"));
            if(resultObj.getInteger("statusCode") == 200){
                result.setResult(resultObj.getJSONObject("result"));
            }else{
                result.setSuccess(false);
            }
        }catch (Exception ex){
            result.setSuccess(false);
            result.error500(ex.getMessage());
        }
        return result;
    }

    @GetMapping("session")
    @ApiOperation("检查token是否有效")
    public Result<JSONObject> checkSession(@RequestParam String token) throws Exception{
        Result<JSONObject> result = new Result<JSONObject>();
        try{
            Integer statusCode = iMstrApiService.session(token);
            if(statusCode == 200){
                result.setSuccess(true);
                result.setMessage("token有效!");
            }else{
                result.setCode(statusCode);
                result.setSuccess(false);
                result.setMessage("token失效!");
            }
        }catch (Exception ex){
            result.setSuccess(false);
            result.error500(ex.getMessage());
        }
        return result;
    }

    @PostMapping("vehicleTypeList")
    @ApiOperation("获取车型列表")
    public Result<JSONArray> vehicleTypeList(@RequestBody Mstr mstr) throws Exception{
        Result<JSONArray> result = new Result<JSONArray>();
        try{
            JSONObject jsonResult = iMstrApiService.getVehicleTypeList(mstr.getToken(), mstr.getProjectId(), mstr.getReportId(), mstr.getAttributeId());
            result.setCode(jsonResult.getInteger("statusCode"));
            if(jsonResult.getInteger("statusCode") == 200){
                result.setSuccess(true);
                result.setResult(jsonResult.getJSONArray("result"));
            }else{
                result.setSuccess(false);
            }
        }catch (Exception ex){
            result.setSuccess(false);
            result.error500(ex.getMessage());
        }
        return result;
    }
}
